package com.springCrud.simpleCrud.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.sql.Blob;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.multipart.MultipartFile;

import com.springCrud.simpleCrud.util.Constraint;
import com.springCrud.simpleCrud.util.FlashMap;
import com.springCrud.simpleCrud.util.FlashMap.Message;
import com.springCrud.simpleCrud.util.FlashMap.MessageType;

public abstract class BaseController {
	
	protected final Log logger = LogFactory.getLog(this.getClass());
	
	protected ResourceBundle configurationResource = ResourceBundle.getBundle("configuration");
	
	protected String getApplicationUrl() {
		return configurationResource.getString("app.url");
	}
	
	protected void addMessage(ModelMap modelMap, String message, MessageType type, boolean isRedirect) {
		if(isRedirect) {
			switch (type) {
				case info:
					FlashMap.setInfoMessage(message);
					break;
					
				case success:
					FlashMap.setSuccessMessage(message);
					break;
					
				case warning:
					FlashMap.setWarningMessage(message);
					break;
					
				case error:
					FlashMap.setErrorMessage(message);
					break;
			}
			
		} else {
			modelMap.put("message", new Message(type, message));
		}
	}
	
	protected String getRealPath(HttpSession session) {
		return session.getServletContext().getRealPath("/");
	}
	
	protected String getOriginalFilename(MultipartFile multipartFile) {
		if(multipartFile.isEmpty()) {
			return null;
		}
		return multipartFile.getOriginalFilename();
	}
	
	protected void uploadFileToServer(MultipartFile multipartFile, String path) throws Exception {
		
		File file = new File(path);
		
		File directory = file.getParentFile();
		if(!directory.exists()) {
			directory.mkdirs();
		}
		file.createNewFile();
		
		FileOutputStream outputStream = new FileOutputStream(file);
		
		outputStream.write(multipartFile.getBytes());
		outputStream.close();
	}

	protected Constraint resolveSearch(String searchMethod, Object example) {
		if(example == null) {
			return null;
		}
		
		Constraint constraint = new Constraint();
		
		StringBuffer buffer = new StringBuffer();
		Map<String, Object> parameters = constraint.getParameters();
		
		// default
		String operator = " LIKE ";
		String prefix = "%";
		String suffix = "%";
		
		specificSearch(example, buffer, parameters, operator, prefix, suffix);
		
		constraint.setWhereClause(buffer.toString());
		constraint.setParameters(parameters);
		
		return constraint;
	}
	
	protected void specificSearch(Object object, StringBuffer buffer, Map<String, Object> parameters, 
			String operator, String prefix, String suffix) {
		// implemented at child class..
	}
	
	
	/* Logging
	
	public void createLog(String performer, HttpServletRequest request, 
			String type, String target, String activity, String before, String after) {
		
		if (StringUtils.equals("anonymousUser", performer)) {
			return ;
		}
		
		SecLog secLog = new SecLog();
		secLog.setPerformer(performer);
		secLog.setTimestamp(new Date());
		if (request != null) {
			secLog.setRemoteAddr(request.getRemoteAddr());
			secLog.setRemoteHost(request.getRemoteHost());
			secLog.setRemoteUser(request.getRemoteUser());
		}
		secLog.setType(type);
		secLog.setTarget(target);
		secLog.setActivity(activity);
		secLog.setBefore(before);
		secLog.setAfter(after);
		
		userManagementService.saveLog(secLog);
	} */
	
}
